//
//  PPCheckMobileViewController.m
//  pet
//
//  Created by 陈无忌 on 1/9/15.
//  Copyright (c) 2015 whalefin. All rights reserved.
//

#import "PPCheckMobileViewController.h"
#import "PPResetPasswordViewController.h"
#import "supportingHeadFiles.h"

@interface PPCheckMobileViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) NSTimer *countDownTimer;
@property (nonatomic) int secondsCountDown;
@property (nonatomic) BOOL canGet;

@property (weak, nonatomic) IBOutlet UIView *locationNoView;
@property (weak, nonatomic) IBOutlet UILabel *locationNoLabel;
@property (weak, nonatomic) IBOutlet UIView *teleView;
@property (weak, nonatomic) IBOutlet UITextField *teleTextFiled;


@property (weak, nonatomic) IBOutlet UIView *verificationCodeView;
@property (weak, nonatomic) IBOutlet UILabel *buttonLabel;
@property (weak, nonatomic) IBOutlet UITextField *verificationCodeTextField;

@end

@implementation PPCheckMobileViewController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.secondsCountDown = 60;
    self.canGet = YES;
    self.buttonLabel.text = @"获取验证码";
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.verificationCodeTextField.text = @"";
    self.teleTextFiled.text = @"";
    [self.countDownTimer invalidate];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self staticViewStyleSet];
}

- (void)staticViewStyleSet
{
    //location No. view
    self.locationNoView.layer.cornerRadius = 14;
    [self.locationNoView.layer setBorderColor:[UIColor colorWithRed:194/255.0 green:193/255.0 blue:193/255.0 alpha:1.0].CGColor];
    self.locationNoView.layer.borderWidth = 1;
    //tele View
    self.teleView.layer.cornerRadius = 14;
    [self.teleView.layer setBorderColor:[UIColor colorWithRed:194/255.0 green:193/255.0 blue:193/255.0 alpha:1.0].CGColor];
    self.teleView.layer.borderWidth = 1;
    //verificationCode View
    self.verificationCodeView.layer.cornerRadius = 14;
    [self.verificationCodeView.layer setBorderColor:[UIColor colorWithRed:194/255.0 green:193/255.0 blue:193/255.0 alpha:1.0].CGColor];
    self.verificationCodeView.layer.borderWidth = 1;
    //delegate
    self.teleTextFiled.delegate = self;
    self.verificationCodeTextField.delegate = self;
}

-(void)timeFireMethod
{
    self.secondsCountDown --;
    self.buttonLabel.text = [NSString stringWithFormat:@"%d秒后重新获取",self.secondsCountDown];
    //    NSLog(@"%d",self.secondsCountDown);
    if (self.secondsCountDown == 0) {
        [self.countDownTimer invalidate];
        self.canGet = YES;
        self.secondsCountDown = 60;
        self.buttonLabel.text = @"获取验证码";
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)back:(id)sender {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)next:(id)sender {
    [self.verificationCodeTextField resignFirstResponder];
    [self.teleTextFiled resignFirstResponder];
    if (self.verificationCodeTextField.text.length > 0 && self.teleTextFiled.text.length == 11) {
        NSString *url = [PPAPIList getAPIList].PPCheckVerifyCode;
        NSDictionary *params = @{@"code":self.verificationCodeTextField.text};
        [httpRequst POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSString *msg = responseObject[@"errorMsg"];
            NSNumber *result = responseObject[@"processResult"];
            if (result.boolValue) {
                PPResetPasswordViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PPResetPasswordViewController"];
                targetVC.phoneNum = self.teleTextFiled.text;
                [self.navigationController pushViewController:targetVC animated:YES];
            }else{
                [PPHUD hudShowWithString:msg andView:self.view];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [PPHUD hudShowWithString:@"网络不给力" andView:self.view];
        }];
    }else{
        [PPHUD hudShowWithString:@"请确保输入正确" andView:self.view];
    }
    
    
}

- (IBAction)getVerificationCodeButton:(id)sender {
    if (self.canGet) {
        self.canGet = NO;
        self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
        //get VerifyCode
        NSString *url = [PPAPIList getAPIList].PPCreateVerifyCode;
        NSDictionary *params = @{@"phoneNum":self.teleTextFiled.text};
        [httpRequst POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSString *msg = responseObject[@"errorMsg"];
            [PPHUD hudShowWithString:msg andView:self.view];
        }failure:^(AFHTTPRequestOperation *operation, NSError *error){
            [PPHUD hudShowWithString:@"网络不给力" andView:self.view];
        }];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self.verificationCodeTextField isExclusiveTouch]) {
        [self.verificationCodeTextField resignFirstResponder];
    }
    if (![self.teleTextFiled isExclusiveTouch]) {
        [self.teleTextFiled resignFirstResponder];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
